#!/usr/bin/python

import sys
import matplotlib.pyplot as plt
import pylab
import numpy as np

contigs454 = sys.argv[1]
f = open(contigs454, "rU")
fl = f.readlines()
contigsSolexa = sys.argv[2]
g = open(contigsSolexa, "rU")
gl = g.readlines()

line = fl[4]
l = line.split('\t')
genome_size = int(l[7])

genome_coords = {}

x1 = []
x2 = []
y1 = []
y2 = []
x3 = []
x4 = []
y3 = []
y4 = []

for i in range(1, genome_size+1):
    genome_coords[i] = 0

for i, line in enumerate(fl[4:]):
    l = line.split('\t')
    start = int(l[0])
    stop = int(l[1])
    gs = int(l[7])
    identity = float(l[6])
    x1.append(start)
    x2.append(stop)
    y1.append(identity)
    y2.append(identity)
    for a in range(start, stop):
        genome_coords[a] = genome_coords[a] + 1

for i, line in enumerate(gl[4:]):
    l = line.split('\t')
    start = int(l[0])
    stop = int(l[1])
    gs = int(l[7])
    identity = float(l[6])
    x3.append(start)
    x4.append(stop)
    y3.append(identity/2)
    y4.append(identity/2)
    for a in range(start, stop):
        genome_coords[a] = genome_coords[a] + 1

cov = []

for k, v in genome_coords.iteritems():
    cov.append(v)

gx = [1,genome_size]
gy = [10,10]

a = [x1, x2]
b = [y1, y2]
c = [x3, x4]
d = [y3, y4]

xcoords = range(1, genome_size+1)

print "xcoords: ", len(xcoords)
print "cov: ", len(cov)

fig = plt.figure(1, figsize=(14,6))

plt.subplots_adjust(wspace=2.0)

plt.subplot(211)
plt.plot(gx, gy, color='blue', marker='|', markersize=8.0,
    markeredgecolor='black', linestyle='-', linewidth=2.0)
plt.plot(a, b, color='purple', linestyle='-')
plt.annotate('454 contigs', xy=(0.8, 0.85), xycoords='axes fraction', horizontalalignment='center', verticalalignment='center')
plt.plot(c, d, color='#996600', linestyle='-')
plt.annotate('Solexa contigs', xy=(0.8, 0.35), xycoords='axes fraction', horizontalalignment='center', verticalalignment='center')
plt.axis([0, genome_size, 0, 110])
plt.title('Recruitment plot')
plt.ylabel('% Identities')
plt.grid(True)

plt.subplot(212)
plt.plot(xcoords, cov, color='#CC9999', linestyle='-')
plt.fill_between(xcoords, cov, facecolor='#CC9999', alpha=0.5)
plt.axis([0, genome_size, 0, max(cov)+2])
plt.title('Recruitment coverage')
plt.xlabel('Genome coordinates(bp)')
plt.ylabel('Counts')
plt.grid(True)

plt.show()
#plt.savefig(outfile, format='pdf')
